<?php
// $Id: views_handler_field_data_markup.inc,v 1.1.2.1 2010/03/03 17:11:28 alexb Exp $

/**
 * A handler to run a field through check_markup.
 */
class views_handler_field_data_markup extends views_handler_field {
  
  function option_definition() {
    $options = parent::option_definition();

    $format_default = $this->_format_default_get();
    $options['format'] = array('default' => $format_default);

    return $options;
  }


  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);

    $options = array('' => t('<Default>'));
    $formats = filter_formats();
    foreach ($formats as $key => $row) {
      $options[$row->format] = $row->name;
    }

    $form['format'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => t('Format'),
      '#default_value' => isset($this->options['format']) ? $this->options['format'] : '',
      '#description' => t('Choose the filter format to use for this field.'),
    );
  }

  function render($values) {
    $value = $values->{$this->field_alias};

    if ($value) {
      $format = isset($this->options['format']) ? $this->options['format'] : '';
      
      if (empty($format)) {
       return check_plain($text);
      }
      else {
       return check_markup($value, $format, $langcode = '' /* TODO Set this variable. */, FALSE);
      }
    }
  }

  function _format_default_get() {
    static $format_default = NULL;

    if (!is_null($format_default)) {
      return $format_default;
    }


    $format_default = filter_fallback_format();
    $formats = filter_formats();
    
    if(!array_key_exists($format_default, $formats)){
      $format_default = '';
    }

    return $format_default;

  }
}
